/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.restklient;

import com.fasterxml.jackson.databind.JsonNode;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

public class TabuleRestKlient {

    public static void main(String[] args) {
        RestTemplate rt = new RestTemplate();
        URI uri = null;

        Map<String, String> map = new HashMap<String, String>();

        if ((args.length == 0 ) || ("help".equals(args[0])) || args.length > 4) {
            printHelp();
        } else {
            if (!(("author".equals(args[0]) || "genre".equals(args[0]))
                    && ("create".equals(args[1]) || "update".equals(args[1]) || "delete".equals(args[1])
                    || "getall".equals(args[1]))  )) {
                System.out.println("Invalid parameters: " + args[0] + ", " + args[1] + ".");
                printHelp();
                return;
            }
            if (args.length == 2 && "getall".equals(args[1])) {
                map.put(args[0] + args[1], args[1]);
            }
            
            if (args.length == 3 && ("create".equals(args[1]) || "delete".equals(args[1])) ){
                map.put(args[0] + args[1], args[2]);
            }
            if (args.length == 4 && "update".equals(args[1])) {
                map.put(args[0]+args[1], args[2]+":"+args[3]);
            } 
            if(map.isEmpty()) {
                System.out.println("Invalid parameters.");
                printHelp();
            }
        
        try {
            uri = rt.postForLocation("http://localhost:8080/pa165/remote/", map);
        } catch (ResourceAccessException ex) {
            System.out.println("Server unavailable.");
        } catch (HttpClientErrorException ex) {
            System.out.println("Server unavailable.");
        }
        JsonNode jsonNode = rt.getForObject("http://localhost:8080/pa165/remote/", JsonNode.class);
        System.out.println("Last action performed: " + jsonNode.get(jsonNode.size()-1));
        
        }

    }
    
    private static void printHelp(){
        System.out.println("HELP");
        System.out.println("Usage of MusicLibraryRestClient: ");
        System.out.println("mvn exec:java author create NAME - creates new author with name NAME,");
        System.out.println("mvn exec:java author getall - returns all authors from database,");
        System.out.println("mvn exec:java author update ID NAME - updates author with id ID to new name NAME,");
        System.out.println("mvn exec:java author delete ID - deletes author with id ID,");
        System.out.println("mvn exec:java genre create NAME - cretes genre with name NAME,");
        System.out.println("mvn exec:java genre getall - returns all genres from database,");
        System.out.println("mvn exec:java genre update ID NAME - updates genre with id ID to new name NAME,");
        System.out.println("mvn exec:java genre delete ID - deletes genre with id ID.");
    }
}